package com.jiyun.play.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.OrderDetails;
import com.domain.Play;
import com.jiyun.play.mapper.OrderDetailsMapper;
import com.jiyun.play.service.PlayService;
import com.spyx.common.result.Result;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/play")
@CrossOrigin
public class PlayController {
    @Autowired
    private PlayService playService;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @PostMapping("/list")
    public Result list(@RequestBody Play play) {
        List<Play> plays = playService.findAll(play);
        return Result.ok(plays);
    }
    @GetMapping("/Tab")
    public Result Tab(@RequestParam("id") Integer id) {
        List<Play> plays = playService.findTab(id);
        return Result.ok(plays);
    }

    @RequestMapping("update")
    public Result update(@RequestBody Play play){
        Play id = playService.getOne(new QueryWrapper<Play>().eq("id", play.getId()));
        playService.updateById(play);
        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setStatus(0);
        orderDetails.setUid(play.getId());
        orderDetails.setCreatetime(LocalDateTime.now());
        orderDetails.setPrice(id.getBalance().subtract(play.getBalance()));
        orderDetailsMapper.insert(orderDetails);
        return Result.ok();
    }
}
